package com.xsdl.flyweight.buffer;

import java.nio.ByteBuffer;
import java.util.concurrent.TimeoutException;

public class Main {

    public static void main(String[] args) throws InterruptedException, TimeoutException {
        BufferPool bufferPool = new BufferPool(1024, 64);

        new Thread(() -> {
            try {
                ByteBuffer buffer = bufferPool.allocate(64, 1000);
                System.out.println(buffer.capacity());
                Thread.sleep(1000);
                bufferPool.deallocate(buffer);
            } catch (InterruptedException | TimeoutException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                ByteBuffer buffer = bufferPool.allocate(900, 1000);
                System.out.println(buffer.capacity());
                Thread.sleep(3000);
                bufferPool.deallocate(buffer);
            } catch (InterruptedException | TimeoutException e) {
                e.printStackTrace();
            }
        }).start();

        ByteBuffer buffer = bufferPool.allocate(1000, 500);
        System.out.println(buffer.capacity());
        bufferPool.deallocate(buffer);
    }

}
